/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.decoder;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author hjp
 */


public class ID3Decoder {
    private static String songName;
    private static String artist;
    private static String album;
    private static int time;
    private static long size;
    /**
     * ID3头部解码器，获取MP3文件相关信息
     */
    public static void decode(String filePath){
        MP3File mp3File;
        try {
            mp3File = (MP3File) AudioFileIO.read(new File(filePath));
            AbstractID3v2Tag v2tag = mp3File.getID3v2Tag();
            //String artist = v2tag.getFirst(ID3v24Frames.FRAME_ID_ARTIST);
            //String album = v2tag.getFirst(ID3v24Frames.FRAME_ID_ALBUM);
            songName = v2tag.getFirst(FieldKey.TITLE);
            artist = v2tag.getFirst(FieldKey.ARTIST);
            album = v2tag.getFirst(FieldKey.ALBUM);
            MP3AudioHeader header = mp3File.getMP3AudioHeader(); // mp3文件头部信息
            time = header.getTrackLength();//获取时长
            size = mp3File.getFile().length();//获取歌曲大小。以字节为单位
        } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
            e.printStackTrace();
            songName = null;
            artist = null;
            album = null;
            time = 0;
            size = 0;
        }
    }

    public static String getSongName(){
        return songName;
    }

    public static String getArtist() {
        return artist;
    }

    public static String getAlbum() {
        return album;
    }

    public static int getTime() {
        return time;
    }

    public static long getSize() {
        return size;
    }
    
    public static int getv2tagSize(String path){
        try {
            MP3File mp3File = (MP3File) AudioFileIO.read(new File (path));
            AbstractID3v2Tag v2tag = mp3File.getID3v2Tag();
            return v2tag.getSize();
        } catch (CannotReadException ex) {
            Logger.getLogger(ID3Decoder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ID3Decoder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TagException ex) {
            Logger.getLogger(ID3Decoder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ReadOnlyFileException ex) {
            Logger.getLogger(ID3Decoder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAudioFrameException ex) {
            Logger.getLogger(ID3Decoder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
//    public static void main(String[] args){
////        File []files = new File("D:\\CloudMusic").listFiles();
////        for(File file : files)
////        {
////            ID3Decoder.decode(file.getAbsolutePath());
////            System.out.println(songName + " " + artist);
////        }
////            ID3Decoder.decode("D:\\CloudMusic\\a.mp3");
////            System.out.println(time);
////            File file = new File("D:\\TEST2\\Test\\形容.Mp3");
////            System.out.println(file.getParent());
//    }
}
